package dk.eclipse.android.badassapp.sensor;

import java.util.HashMap;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import dk.eclipse.android.badassapp.R;
import dk.eclipse.android.badassapp.sensor.accelerometer.AccelerometerActivity;
import dk.eclipse.android.badassapp.sensor.gyroscope.GyroscopeActivity;
import dk.eclipse.android.badassapp.sensor.light.LightActivity;
import dk.eclipse.android.badassapp.sensor.magneticfield.MagneticFieldActivity;
import dk.eclipse.android.badassapp.sensor.orientation.OrientationActivity;
import dk.eclipse.android.badassapp.sensor.pressure.PressureActivity;
import dk.eclipse.android.badassapp.sensor.proximity.ProximityActivity;
import dk.eclipse.android.badassapp.sensor.temperature.TemperatureActivity;

/**
 * This activity explores the sensor API and services.
 * 
 * @author Ronni Kahalani (eclipse.dk)
 */
public class SensorActivity extends ListActivity {

	/**
	 * Tag used for logging information.
	 */
	private static final String LOG_TAG = SensorActivity.class
			.getSimpleName();

	private static Context CONTEXT;

	private List<Sensor> sensorList;
	private HashMap<Integer, Class<?>> sensorActivities;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.sensors);
		setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_app);

		CONTEXT = this;
		
		sensorActivities = new HashMap<Integer, Class<?>>();
		sensorActivities.put(Integer.valueOf(Sensor.TYPE_ACCELEROMETER), AccelerometerActivity.class);
		sensorActivities.put(Integer.valueOf(Sensor.TYPE_GYROSCOPE), GyroscopeActivity.class);
		sensorActivities.put(Integer.valueOf(Sensor.TYPE_LIGHT), LightActivity.class);
		sensorActivities.put(Integer.valueOf(Sensor.TYPE_MAGNETIC_FIELD), MagneticFieldActivity.class);
		sensorActivities.put(Integer.valueOf(Sensor.TYPE_ORIENTATION), OrientationActivity.class);
		sensorActivities.put(Integer.valueOf(Sensor.TYPE_PRESSURE), PressureActivity.class);
		sensorActivities.put(Integer.valueOf(Sensor.TYPE_PROXIMITY), ProximityActivity.class);
		sensorActivities.put(Integer.valueOf(Sensor.TYPE_TEMPERATURE), TemperatureActivity.class);
		
		
		// Get the SensorManager
		SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

		sensorList = sensorManager
				.getSensorList(Sensor.TYPE_ALL);
		SensorAdapter sensorAdapter = new SensorAdapter(this,
				R.layout.sensor_row_layout, sensorList);
		setListAdapter(sensorAdapter);
		
		
		Button showSensorVisualizerButton = (Button) findViewById(R.id.showSensorVisualizerButton);
		showSensorVisualizerButton.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), SensorVisualizerActivity.class);
				startActivity(intent);
			}
		});
		
		
		Button showCompassButton = (Button) findViewById(R.id.showCompassButton);
		showCompassButton.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), CompassActivity.class);
				startActivity(intent);
			}
		});
		
		
		
		
	}

	protected void onResume() {
		super.onResume();
	}

	protected void onDestroy() {
		super.onDestroy();
	}

	public static Context getContext() {
		return CONTEXT;
	}


	private class SensorAdapter extends ArrayAdapter<Sensor> {

		private List<Sensor> items;

		public SensorAdapter(Context context, int textViewResourceId,
				List<Sensor> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.sensor_row_layout, null, true);
			}
			
			Sensor sensor = items.get(position);
			if (sensor != null) {
				TextView titleTextView = (TextView) v.findViewById(R.id.sensorListTitleTextView);
				titleTextView
						.setText(String.format("%s v.%d", sensor.getName(), sensor.getVersion()));

				TextView summaryTextView = (TextView) v.findViewById(R.id.sensorListSummaryTextView);
				summaryTextView
						.setText(String.format("%s (%s)", sensor.getVendor(), getSensorTypeName(sensor)));

			}
			return v;
		}
	}

	
	public String getSensorTypeName(Sensor sensor) {
		String typeName = null;

		switch (sensor.getType()) { // Check the type of Sensor that generate
									// the event
		// and show is resolution
		case Sensor.TYPE_ACCELEROMETER:
			typeName = getString(R.string.sensorHardwareTypeAccelerometer);
			break;
		case Sensor.TYPE_GYROSCOPE:
			typeName = getString(R.string.sensorHardwareTypeGyroscope);
			break;
		case Sensor.TYPE_LIGHT:
			typeName = getString(R.string.sensorHardwareTypeLight);
			break;
		case Sensor.TYPE_MAGNETIC_FIELD:
			typeName = getString(R.string.sensorHardwareTypeMagneticField);
			break;
		case Sensor.TYPE_ORIENTATION:
			typeName = getString(R.string.sensorHardwareTypeOrientation);
			break;
		case Sensor.TYPE_PRESSURE:
			typeName = getString(R.string.sensorHardwareTypePressure);
			break;
		case Sensor.TYPE_PROXIMITY:
			typeName = getString(R.string.sensorHardwareTypeProximity);
			break;
		case Sensor.TYPE_TEMPERATURE:
			typeName = getString(R.string.sensorHardwareTypeTemperature);
			break;

		default:
			typeName = getString(R.string.sensorHardwareTypeUnknown);
			break;
		}

		return typeName;
	}

	/* (non-Javadoc)
	 * @see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		Sensor sensor = sensorList.get((int) id);
		int type = sensor.getType();
		Intent intent = new Intent(this, sensorActivities.get(type));
		startActivity(intent);
		
	}

	
}